<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/23
 * Time: 17:46
 */

namespace app\modules\backed\controllers;

/**
 *
 * 后台登录页面
 */

use app\models\Account;
use yii\helpers\Url;
use yii\web\Controller;

class LoginController extends Controller
{
      public $enableCsrfValidation=false;
    /**
     * @return string
     * 登陆页
     */
    public function actionIndex()
    {
        return $this->renderPartial('index');
    }

    /**
     *登陆认证
     */
    public function actionSubmit()
    {
        $accountMode = new Account();
        $request = \Yii::$app->request;
        $postData = $request->post();
        $customers = $accountMode::find()
            ->where(['user_name' => $postData['username']])
            ->asArray()
            ->one();
        if (!$customers) {
            echo json_encode(array('code' => 1, 'msg' => '账号不存在'));
            die();
        }
        $tempPassword = md5($postData['password'] . $customers['salt']);
        if ($customers['password'] != $tempPassword) {
            echo json_encode(array('code' => 1, 'msg' => '账号密码错误'));
            die();
        }
        $session = \Yii::$app->session;
        // 开启session
        if ($session->isActive) {
            $session->open();
        }
        $session->set('account_id', $customers['id']);
        $session->set('account_data', $customers);
        echo json_encode(array('code' => 2, 'msg' => '登录成功'));
    }

    /**
     * 登出
     */
    public function actionOut()
    {
        $session = \Yii::$app->session;
        // 关闭session
        $session->close();
        // 销毁session中所有已注册的数据
        $session->destroy();
        $this->redirect(Url::toRoute('login/index'));
    }
}